package com.ruoyi.security.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.security.domain.AfChemicals;
import com.ruoyi.security.domain.vo.AfChemicalsVo;
import com.ruoyi.security.service.IAfChemicalsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 危化品Controller
 *
 * @author felix
 * @date 2021-10-12
 */
@Api(tags = " 危化品")
@RestController
@RequestMapping("/afChemicals")
public class AfChemicalsController extends BaseController {
    @Autowired
    private IAfChemicalsService afChemicalsService;

    /**
     * 查询危化品列表
     */
    @ApiOperation(value = "查询危化品列表", notes = "查询危化品列表")
    @PreAuthorize(hasPermi = "security:afChemicals:list")
    @GetMapping("/list")
    public TableDataInfo list(AfChemicals afChemicals) {
        startPage();
        List<AfChemicals> list = afChemicalsService.selectAfChemicalsList(afChemicals);
        return getDataTable(list);
    }


    /**
     * 小程序查询危化品列表
     */
    @ApiOperation(value = "小程序查询危化品列表", notes = "小程序查询危化品列表")
    @PreAuthorize(hasPermi = "security:afChemicals:list")
    @GetMapping("/appList")
    public AjaxResult appList(AfChemicalsVo vo) {
        AfChemicals afChemicals = new AfChemicals();
        BeanUtils.copyProperties(vo, afChemicals);
        List<AfChemicals> list = afChemicalsService.selectAfChemicalsList(afChemicals);
        List<AfChemicalsVo> result = new ArrayList<>();
        list.stream().forEach(o -> {
            AfChemicalsVo v = new AfChemicalsVo();
            BeanUtils.copyProperties(o, v);
            result.add(v);
        });
        return AjaxResult.success(result);
    }

    /**
     * 导出危化品列表
     */
    @ApiOperation(value = "导出危化品列表", notes = "导出危化品列表")
    @PreAuthorize(hasPermi = "security:afChemicals:export")
    @Log(title = "危化品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AfChemicals afChemicals) throws IOException {
        List<AfChemicals> list = afChemicalsService.selectAfChemicalsList(afChemicals);
        ExcelUtil<AfChemicals> util = new ExcelUtil<AfChemicals>(AfChemicals.class);
        util.exportExcel(response, list, "危化品数据");
    }

    /**
     * 获取危化品详细信息
     */
    @ApiOperation(value = "获取危化品详细信息", notes = "获取危化品详细信息")
    @PreAuthorize(hasPermi = "security:afChemicals:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(afChemicalsService.selectAfChemicalsById(id));
    }

    /**
     * 新增危化品
     */
    @ApiOperation(value = "新增危化品", notes = "新增危化品")
    @PreAuthorize(hasPermi = "security:afChemicals:add")
    @Log(title = "危化品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AfChemicals afChemicals) {
        return toAjax(afChemicalsService.insertAfChemicals(afChemicals));
    }

    /**
     * 修改危化品
     */
    @ApiOperation(value = "修改危化品", notes = "修改危化品")
    @PreAuthorize(hasPermi = "security:afChemicals:edit")
    @Log(title = "危化品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AfChemicals afChemicals) {
        return toAjax(afChemicalsService.updateAfChemicals(afChemicals));
    }

    /**
     * 删除危化品
     */
    @ApiOperation(value = "删除危化品", notes = "删除危化品")
    @ApiImplicitParam(name = "ids", value = "危化品ID数组", required = true, paramType = "path", allowMultiple = true, dataType = "String")
    @PreAuthorize(hasPermi = "security:afChemicals:remove")
    @Log(title = "危化品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(afChemicalsService.deleteAfChemicalsByIds(ids));
    }
}
